// 公共方法 


//数组去重
Array.prototype.unique = function(){
 var res = [];
 var json = {};
 for(var i = 0; i < this.length; i++){
  if(!json[this[i]]){
   res.push(this[i]);
   json[this[i]] = 1;
  }
 }
 return res;
}

//数组去除指定项
Array.prototype.remove = function(val) {
	for(var i=0; i<this.length; i++) {
	    if(this[i] == val) {
	      this.splice(i, 1);
	      break;
	    }
	}
}

//数组最小值
Array.prototype.min = function() {
	var min = this[0];
	var len = this.length;
	for (var i = 1; i < len; i++){ 
		if (this[i] < min){ 
			min = this[i]; 
		} 
	} 
	return min;
}

//数组最大值
Array.prototype.max = function() { 
	var max = this[0];
	var len = this.length; 
	for (var i = 1; i < len; i++){ 
		if (this[i] > max) { 
			max = this[i]; 
		} 
	} 
	return max;
}

//检查某个元素在数组中的索引值
Array.prototype.indexOf = function(el){

	 for (var i=0,n=this.length; i<n; i++){

	  if (this[i] === el){

	   return i;

	  }
	 }
	 return -1;
}

	
// 使用ES6 class新方法重写构造函数  使用方法等同于常规构造函数
class timers{
	// 时间戳转时间
	formatDate(now){   
		var now = new Date(now*1000)  
		return now.toLocaleString()
	}
	// 时间戳转时间
	toTimeTamp(time){
		var date = new Date(time); //时间对象
		console.log(date)
		var str = date.getTime()/1000; //转换成时间戳
		return str;
	}    
}





//获取两个日期间的所有日期
/*************************
 * 计算两个日期时间段内所有日期 
 *  
 * @param value1 
 *            开始日期 YYYY/MM/DD 
 * @param value2 
 *            结束日期 
 * return 日期数组 
 */  
function dataScope(value1, value2) {  
	function addZero(val){
		console.log(val);
		if(val.length==1){
			var str = "0"+val;
			return str;
		}else{
			return val;
		}
	}
	if(value1==value2){
		var arr = [];
		var d1 = value1.split("/");
	    d1[1] = addZero(d1[1]);
	    d1[2] = addZero(d1[2]);
		arr.push(d1.join("-"));
		return arr;
	}else{
		var getDate = function(str) {  
	        var tempDate = new Date();  
	        var list = str.split("/");  
	        tempDate.setFullYear(list[0]);  
	        tempDate.setMonth(list[1] - 1);  
	        tempDate.setDate(list[2]);  
	        return tempDate;  
	    }  
	    var date1 = getDate(value1);  
	    console.log(date1);
	    
	    var date2 = getDate(value2);  
	    console.log(date2);
	    if (date1 > date2) {  
	        var tempDate = date1;  
	        date1 = date2;  
	        date2 = tempDate;  
	    }  
	    date1.setDate(date1.getDate() + 1);  
	    var dateArr = [];  
	    var i = 0;  
	    while (!(date1.getFullYear() == date2.getFullYear()  
	            && date1.getMonth() == date2.getMonth() && date1.getDate() == date2  
	            .getDate())) {  
	        var dayStr = addZero(date1.getDate().toString());  

	        var monthStr = addZero(String(date1.getMonth() + 1));
	            

	        dateArr[i] = date1.getFullYear() + "-" + monthStr + "-"  
	                + dayStr;  	      
	        i++; 
	        date1.setDate(date1.getDate() + 1);  
	    }  
	    var d1 = value1.split("/");
	    d1[1] = addZero(d1[1]);
	    d1[2] = addZero(d1[2]);
	    var d2 = value2.split("/");
	    d2[1] = addZero(d2[1]);
	    d2[2] = addZero(d2[2]);
	    dateArr.unshift(d1.join("-"));
	    dateArr.push(d2.join("-"));	    
	    return dateArr;  	
	} 
}
		
//console.log(dataScope('2017/4/20','2017/4/30'))

//获取前天后天今天明天
function GetDateStr(AddDayCount) { 
	var dd = new Date(); 
	dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期 
	var y = dd.getFullYear(); 
	var m = dd.getMonth()+1;//获取当前月份的日期 
	var d = dd.getDate(); 
	return y+"-"+m+"-"+d; 
	} 
//	document.write("前天："+GetDateStr(-2)); 
	//document.write("<br />昨天："+GetDateStr(-1)); 
	//document.write("<br />今天："+GetDateStr(0)); 
	//document.write("<br />明天："+GetDateStr(1)); 
	//document.write("<br />后天："+GetDateStr(2)); 
	//document.write("<br />大后天："+GetDateStr(3)); 



// 使用ES6方法改写cookie
class Cookies{
	set(name,value){
		var Days = 30
		var exp = new Date()
		exp.setTime(exp.getTime() + Days*24*60*60*1000)
		document.cookie = `${name}=${escape (value)};expires=${exp.toGMTString()}`
	}

	get(name){
		if (document.cookie.length>0){
			var c_start = document.cookie.indexOf(`${name}=`);
			if (c_start>-1) { 
				c_start = c_start + name.length+1;   
				var c_end = document.cookie.indexOf(";",c_start);				        
		        if (c_end>-1) {		        	
	           		return unescape(document.cookie.substring(c_start,c_end));
		        }else{
		        	console.error(`The cookie which named ${name} wasn't exist `)
					return false;			        	   	
		        }
	    	}else{
		    	console.error(`The cookie which named ${name} wasn't exist `)
				return false;
	    	}
		}else{
			console.error(`Can't find any cookies`)
			return false
		}
	}

	del(name){
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval = this.get(name);
		if(cval!=null){
			document.cookie = name + "="+cval+";expires="+exp.toGMTString();
			return true
		}else{
			console.error(`The cookie which named ${name} wasn't exist `)
			return false;
		}	
	}
}



// 对象数组去重
Object.prototype.removal = function(){
	var arrResult = [];
    for(var i = 0; i < this.length; i++) {
        this[i] = JSON.stringify(this[i]); //对象转成字符串的方法
        arrResult.push(this[i]); //重新填装成一个《字符串》组成的数组
    }

    arrResult = arrResult.unique(); // 去掉重复的字符串

    for(var j = 0; j < arrResult.length; j++) {
        arrResult[j] = JSON.parse(arrResult[j]); // 重新给字符串转换成对象
    }
    return arrResult;
}

// 对象属性比较器
function compare(propertyName) {
			return function(object1, object2) {
				var value1 = object1[propertyName];
				var value2 = object2[propertyName];
				if(value2 < value1) {
					return -1;
				} else if(value2 > value1) {
					return 1;
				} else {
					return 0;
				}
			}
		}

// 示例  data.sort(compare("time"));  大的在前 小的在后